using System.Collections.Generic;
using SCore.Util;
using UnityEngine;

namespace Function.SkillEditor.Runtime
{
    public interface IClipRenderEnv
    {
        public Animator _animator { get; }
        public Transform GetBone(string bondName);
        public GameObject _go { get; }
    }

    public struct RenderMessage
    {
        public int _entityId;
        public SkillClip _skillClip;
        public IClipRenderEnv _clipRenderEnv;
        public RenderState _renderState;
    }

    public enum RenderState
    {
        Enter,
        Update,
        Exit
    }


    public class SkillRunRender : Singleton<SkillRunRender>
    {
        /// <summary>
        /// 所有的渲染单元
        /// </summary>
        private Dictionary<int, IClipRenderEnv> _allRenders = new Dictionary<int, IClipRenderEnv>();

        /// <summary>
        /// 渲染队列
        /// </summary>
        public Queue<RenderMessage> _renderMessages = new Queue<RenderMessage>();

        public void Tick(float dt)
        {
            while (_renderMessages.Count > 0)
            {
                var message = _renderMessages.Dequeue();
                message._skillClip.Render(message._renderState, message._clipRenderEnv);
            }
        }

        public void SendMessage(int entityId, RenderState renderState, SkillClip skillClip)
        {
            if (!_allRenders.TryGetValue(entityId, out var env))
            {
                $"SkillRunRender Not Find Env {entityId}".Error();
                return;
            }

            RenderMessage renderMessage = new RenderMessage()
            {
                _entityId = entityId,
                _skillClip = skillClip,
                _clipRenderEnv = env,
                _renderState = renderState,
            };
            _renderMessages.Enqueue(renderMessage);
        }

        public void AddRender(int entityId, IClipRenderEnv render)
        {
            if (_allRenders.ContainsKey(entityId))
            {
                $"SkillRunRender AddRender Error {entityId}".Error();
                return;
            }

            _allRenders.Add(entityId, render);
        }
    }
}